package client.domain.ui.centerPanel;

import client.domain.Chart;
import client.domain.ChartProperty;
import static client.domain.SandboxMainKeys.CENTER_PANEL_WIDTH;
import client.domain.ui.centerPanel.chart.CreateChartWizard;
import client.internationalization.SandboxConstants;
import client.internationalization.SandboxMessages;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.ui.Image;
import com.gwtext.client.core.EventObject;
import com.gwtext.client.core.Function;
import com.gwtext.client.widgets.*;
import com.gwtext.client.widgets.event.ButtonListenerAdapter;
import com.gwtext.client.widgets.form.Label;
import com.gwtext.client.widgets.layout.ColumnLayoutData;
import com.gwtext.client.widgets.layout.FitLayout;
import com.gwtext.client.widgets.layout.VerticalLayout;
import com.gwtext.client.widgets.portal.Portal;
import com.gwtext.client.widgets.portal.PortalColumn;
import com.gwtext.client.widgets.portal.Portlet;

/**
 * User: nbarrios
 * Date: Nov 9, 2009
 */
public class ChartUI extends Panel {

	//	Internationalization Strings
	private SandboxConstants constants = GWT.create(SandboxConstants.class);
	private SandboxMessages messages = GWT.create(SandboxMessages.class);

	//	Main Widgets
	private Portal portal = new Portal();
	private Panel topChartPanel = new Panel();
	private Button createChartButton = new Button(constants.createChart());
	private Window createChartWizardPopUp = null;


	// Constructor
	public ChartUI() {
		super();
		this.setWidth(CENTER_PANEL_WIDTH);
		this.setTitle(constants.charts());
		this.setHeight(500);
		this.setClosable(true);
		this.setBorder(false);
		this.setLayout(new FitLayout());

		//create some shared tools
		Tool gear = new Tool(Tool.GEAR, new Function() {
			public void execute() {
				MessageBox.alert("Message", "The Settings Tool was clicked");
			}
		});

		Tool close = new Tool(Tool.CLOSE, new Function() {
			public void execute() {
				MessageBox.alert("Message", "The Settings Tool was clicked");
			}
		});

		Tool[] tools = new Tool[]{gear, close};



		//	Create a Portal
		portal.setBorder(false);


		//Create portal columns
		PortalColumn topCol = new PortalColumn();
		PortalColumn firstCol = new PortalColumn();
		PortalColumn secondCol = new PortalColumn();

		//	Customize columns
		topCol.setPaddings(10, 10, 10, 10);
		firstCol.setPaddings(10, 10, 10, 10);
		secondCol.setPaddings(10, 10, 10, 10);

		//	Add portlets to portal columns
		//	Top Column
		topChartPanel.setLayout(new VerticalLayout());
		topChartPanel.add(new Label("aca iria un label polenta..."));
		createChartButton.addListener(new ButtonListenerAdapter(){
			public void onClick(Button button, EventObject e) {
//				createChartWizardPopUpUI();
				if(createChartWizardPopUp==null)
					createChartWizardPopUp = new CreateChartWizard();
				else{
					createChartWizardPopUp=null;
					createChartWizardPopUp = new CreateChartWizard();
				}
				
				createChartWizardPopUp.show();
			}
		});
		topChartPanel.add(createChartButton);
		topCol.add(topChartPanel);

		//	First Column
		Portlet firstPortlet = new Portlet();
		Panel aPortletPanel1 = new Panel();
		aPortletPanel1.setLayout(new VerticalLayout());

		Portlet secondPortlet = new Portlet();
		Panel aPortletPanel2 = new Panel();
		aPortletPanel2.setLayout(new VerticalLayout());

		firstPortlet.setTitle(constants.aLineChart());
		firstPortlet.setLayout(new FitLayout());
		firstPortlet.setTools(tools);

		secondPortlet.setTitle(constants.aPieChart());
		secondPortlet.setLayout(new FitLayout());
		secondPortlet.setTools(tools);


		//	Adding charts
		try{

			Chart chartObject = new Chart(Chart.CHART_PARAMETER_TYPE_VALUE_LINE_CHART);
			chartObject.setWidth(300);
			chartObject.setHeight(150);
			chartObject.setDataTextEncoding(Chart.CHART_PARAMETER_DATA_TEXT_ENCODING_VALUE_TEXT);
			chartObject.addProperty(new ChartProperty("June", 20));
			chartObject.addProperty(new ChartProperty("July", 40));
			chartObject.addProperty(new ChartProperty("August", 30));
			chartObject.addProperty(new ChartProperty("September", 80));
			Image lineChart = new Image(chartObject.create());

			aPortletPanel1.add(lineChart);
			aPortletPanel1.add(new Label(constants.lineChartLabel()));


			chartObject = new Chart(Chart.CHART_PARAMETER_TYPE_VALUE_PIE_3D);
			chartObject.setWidth(250);
			chartObject.setHeight(100);
			chartObject.setDataTextEncoding(Chart.CHART_PARAMETER_DATA_TEXT_ENCODING_VALUE_TEXT);
			chartObject.addProperty(new ChartProperty("Hello", 20));
			chartObject.addProperty(new ChartProperty("World", 40));
			Image pieChart = new Image(chartObject.create());

			aPortletPanel2.add(pieChart);
			aPortletPanel2.add(new Label(constants.pieChartLabel()));
		}
		catch(Exception e){
			System.err.println("There was an error trying to connect to the Google Chart Server.");
			e.printStackTrace();
			throw new RuntimeException(e);
		}

		firstPortlet.add(aPortletPanel1);
		secondPortlet.add(aPortletPanel2);

		firstCol.add(firstPortlet);
		firstCol.add(secondPortlet);



		//	Second Column
		Portlet thirdPortlet = new Portlet();
		Panel aPortletPanel3 = new Panel();
		aPortletPanel3.setLayout(new VerticalLayout());

		Portlet fourthPortlet = new Portlet();
		Panel aPortletPanel4 = new Panel();
		aPortletPanel4.setLayout(new VerticalLayout());

		Portlet fifthPortlet = new Portlet();
		Panel aPortletPanel5 = new Panel();
		aPortletPanel5.setLayout(new VerticalLayout());

		thirdPortlet.setTitle(constants.aVennChart());
		thirdPortlet.setLayout(new FitLayout());
		thirdPortlet.setTools(tools);

		fourthPortlet.setTitle(constants.aRadarChart());
		fourthPortlet.setLayout(new FitLayout());
		fourthPortlet.setTools(tools);

		fifthPortlet.setTitle(constants.aMapChart());
		fifthPortlet.setLayout(new FitLayout());
		fifthPortlet.setTools(tools);

		//	Adding charts
		try{
			Chart chartObject = new Chart(Chart.CHART_PARAMETER_TYPE_VALUE_VENN);
			chartObject.setWidth(300);
			chartObject.setHeight(150);
			chartObject.setDataTextEncoding(Chart.CHART_PARAMETER_DATA_TEXT_ENCODING_VALUE_TEXT);
			chartObject.addProperty(new ChartProperty(100));
			chartObject.addProperty(new ChartProperty(60));
			chartObject.addProperty(new ChartProperty(50));
			chartObject.addProperty(new ChartProperty(20));
			chartObject.addProperty(new ChartProperty(45));
			chartObject.addProperty(new ChartProperty(20));
			chartObject.addProperty(new ChartProperty(20));
			Image vennChart = new Image(chartObject.create());

			aPortletPanel3.add(vennChart);
			aPortletPanel3.add(new Label(constants.vennChartLabel()));


			chartObject = new Chart(Chart.CHART_PARAMETER_TYPE_VALUE_RADAR_CURVED_LINES);
			chartObject.setWidth(360);
			chartObject.setHeight(400);
			chartObject.setDataTextEncoding(Chart.CHART_PARAMETER_DATA_TEXT_ENCODING_VALUE_TEXT);
			chartObject.addProperty(new ChartProperty("January", 10));
			chartObject.addProperty(new ChartProperty("February", 20));
			chartObject.addProperty(new ChartProperty("March", 30));
			chartObject.addProperty(new ChartProperty("April", 40));
			chartObject.addProperty(new ChartProperty("May", 10));
			chartObject.addProperty(new ChartProperty("June", 53));
			chartObject.addProperty(new ChartProperty("July", 40));
			chartObject.addProperty(new ChartProperty("August", 80));
			chartObject.addProperty(new ChartProperty("September", 90));
			chartObject.addProperty(new ChartProperty("October", 120));
			chartObject.addProperty(new ChartProperty("November", 122));
			chartObject.addProperty(new ChartProperty("December", 140));
			Image radarChart = new Image(chartObject.create());

			aPortletPanel4.add(radarChart);
			aPortletPanel4.add(new Label(constants.radarChartLabel()));


			chartObject = new Chart(Chart.CHART_PARAMETER_TYPE_VALUE_MAP);
			chartObject.setWidth(440);
			chartObject.setHeight(220);
			chartObject.setDataTextEncoding(Chart.CHART_PARAMETER_DATA_TEXT_ENCODING_VALUE_SIMPLE);
			chartObject.setMapZone(Chart.MAP_ZONE_WORLD);
			Image mapChart = new Image(chartObject.create());

			aPortletPanel5.add(mapChart);
			aPortletPanel5.add(new Label(constants.mapChartLabel()));
		}
		catch(Exception e){
			System.err.println("There was an error trying to connect to the Google Chart Server.");
			e.printStackTrace();
			throw new RuntimeException(e);
		}

		thirdPortlet.add(aPortletPanel3);
		fourthPortlet.add(aPortletPanel4);
		fifthPortlet.add(aPortletPanel5);

		secondCol.add(thirdPortlet);
		secondCol.add(fourthPortlet);
		secondCol.add(fifthPortlet);



		//	Add portal columns to portal
		portal.add(topCol, new ColumnLayoutData(1));
		portal.add(firstCol, new ColumnLayoutData(.5));
		portal.add(secondCol, new ColumnLayoutData(.5));

		this.add(portal);
	}


}//  End Class
